package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.SeekBar;

import androidx.annotation.NonNull;

import com.baihan.angus.app.game.basic.utils.ComUtil;
import com.baihan.angus.app.game.busshow.R;
import com.tencent.liteav.audio.TXAudioEffectManager;

public class GameSettingDialog extends Dialog {

    private Callback mCallback;
    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private LinearLayout llInitiateDissolution, llBgm, llShareRoom, llExitGame;
    private SeekBar sbMicVolume, sbBgmVolume;
    private TXAudioEffectManager mAudioEffectManager;
    private int mBGMVolume = 100;
    private int mBGMId = -1;

    public GameSettingDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
//        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.width = ComUtil.Dp2Px(context, 262);
        mLayoutParams.height = ComUtil.Dp2Px(context, 300);
        dialogWindow.setAttributes(mLayoutParams);
//        dialogWindow.getDecorView().setPadding(80, 0, 80, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.CENTER);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_game_setting, (ViewGroup) null);
        setContentView(dialogView);
        llBgm = dialogView.findViewById(R.id.llBgm);
        llInitiateDissolution = dialogView.findViewById(R.id.llInitiateDissolution);
        llShareRoom = dialogView.findViewById(R.id.llShareRoom);
        llExitGame = dialogView.findViewById(R.id.llExitGame);
        sbMicVolume = dialogView.findViewById(R.id.sbMicVolume);
        sbBgmVolume = dialogView.findViewById(R.id.sbBgmVolume);

        sbMicVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mAudioEffectManager != null) {
                    mAudioEffectManager.setVoiceCaptureVolume(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        sbBgmVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mBGMVolume = progress;
                if (mAudioEffectManager != null && mBGMId != -1) {
                    mAudioEffectManager.setMusicPlayoutVolume(mBGMId, progress);
                    mAudioEffectManager.setMusicPublishVolume(mBGMId, progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        llInitiateDissolution.setOnClickListener(view -> {
            if (null != mCallback) {
                mCallback.onInitiateDissolution();
            }
        });

        llShareRoom.setOnClickListener(view -> {
            if (null != mCallback) {
                mCallback.onShareRoom();
            }
        });

        llExitGame.setOnClickListener(view -> {
            if (null != mCallback) {
                mCallback.onExitGame();
            }
        });

    }

    public void setAudioEffectManager(TXAudioEffectManager audioEffectManager) {
        mAudioEffectManager = audioEffectManager;
    }

    public void setBgmId(int bgmId) {
        mBGMId = bgmId;
    }

    public interface Callback {
        void onInitiateDissolution();

        void onShareRoom();

        void onExitGame();
    }
}
